//
//  RecipientsResultVC.swift
//  ShareUmbrella
//
//  Created by keng fu on 2017/10/10.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import YSKit
import SnapKit
import RxSwift
import KMPlaceholderTextView
import TSMessages

class RecipientsResultVC: YSBaseViewController {
    var textView: KMPlaceholderTextView!
    var startView: TQStarRatingView!
    let publishButton = UIButton()
    let portraitView = UIImageView()
    let telView = UIButton()
    let subtitleLabel = UILabel()
    let titleLabel = UILabel()
    
    public var helpId: String!
    public var nickName: String!
    public var imageHeader: String!
    
    override func buildUI() {
        super.buildUI()
        self.title = "我的帮助"
        self.view.backgroundColor = .white
        
        let topView = UIImageView().ys.customize { (view) in
            self.view.addSubview(view)
            view.image = UIImage(named: "Photo")
            view.snp.makeConstraints({ (make) in
                make.leading.equalTo(self.view.snp.leading).offset(10)
                make.top.equalTo(self.view.snp.top).offset(10)
                make.trailing.equalTo(self.view.snp.trailing).offset(-10)
                make.height.equalTo(view.snp.width).multipliedBy(338.0 / 680)
            })
        }
        
        let startAddress = UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = I18n("VC.et.good title")
            view.textColor = .black
            view.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(topView.snp.bottom).offset(10)
                make.leading.equalTo(self.view).offset(10)
            })
        }
        let endAddress = UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = I18n("VC.et.detail", 15, 5.0)
            view.textColor = .black
            view.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(startAddress.snp.bottom).offset(10)
                make.leading.equalTo(startAddress)
            })
        }
        
        let btnFinish = UIButton().ys.customize { (view) in
            self.view.addSubview(view)
            view.setTitle("帮助成功", for: .normal)
            view.titleLabel?.textColor = .white
            view.setBackgroundImage(UIImage(named: "Rectangle 2"), for: .normal)
            view.titleLabel?.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(endAddress.snp.bottom).offset(20)
                make.centerX.equalTo(self.view)
                make.height.equalTo(60)
                make.width.equalTo(self.view.snp.width).dividedBy(2)
            })
        }
        
        let label3 = UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = "恭喜获得50积分"
            view.textColor = .darkGray
            view.font = YSFont.t03.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(btnFinish.snp.bottom).offset(20)
                make.centerX.equalTo(self.view)
            })
        }
        
        let btnGotoShop = UIButton().ys.customize { (view) in
            self.view.addSubview(view)
            view.setTitle("进入商城", for: .normal)
            view.titleLabel?.textColor = .white
            view.setBackgroundImage(UIImage(named: "Rectangle 3"), for: .normal)
            view.titleLabel?.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(label3.snp.bottom).offset(25)
                make.centerX.equalTo(self.view)
                make.height.equalTo(40)
                make.width.equalTo(self.view.snp.width).dividedBy(3)
            })
        }
        
        _ = btnFinish.rx.tap
            .takeUntil(self.rx.deallocated)
            .subscribe(onNext: { [weak self](RxGestureTypeOptions) in
                
                guard let  `self` = self else {
                    return
                }
                
                self.navigationController?.popToRootViewController(animated: true)
                
            })
        
        _ = btnGotoShop.rx.tap
            .takeUntil(self.rx.deallocated)
            .subscribe(onNext: { [weak self](RxGestureTypeOptions) in
                
                guard let  `self` = self else {
                    return
                }
                
                let vc = IntegralShopVC()
                self.ys.push(vc)

                
            })
        
        _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(UserAPI.helpInfo(token: Auth.token, helpid: self.helpId))).subscribe(onNext: { (data) in
            
            guard let `data` = data else {
                return
            }
            
            startAddress.text =  data["data"]["from"]["name"].stringValue
            endAddress.text = data["data"]["to"]["name"].stringValue
            label3.text =  String(format: "恭喜获得%@积分", data["data"]["score"].stringValue)
            
        }, onError: {(ErrorType) in
            HUD.hide()
            
            TSMessage.showNotification(withTitle: "获取信息失败", type: .error)
        })
    }
    
    override func buildRx() {
        super.buildRx()
    }
    
    open override func beforePush(_ withdata:Any?){
        self.helpId = withdata as! String
    }
}

